package ccuserinterface;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import datamodels.Conference;
import datamodels.GlobalDataStore;
import datamodels.Role;
import datamodels.User;
/**
 * A frame for assigning a reviewer to a manuscript.
 * @author anthonysierra
 *
 */
@SuppressWarnings("serial")
public class AssignReviewerToManuscript extends JFrame
{
	JComboBox my_users = new JComboBox();
	JComboBox my_manuscripts = new JComboBox();
	JTable my_jtable;
	JScrollPane my_scroll_pane;
	GlobalDataStore my_data_store;
	JButton my_back_btn;
	Role my_role;
	Conference my_conference;
	User my_user;
	String current_reviewer_name = new String();
	String current_manuscript_name = new String();
	public AssignReviewerToManuscript(GlobalDataStore the_data_store, Role the_role, Conference the_conference, User the_user)
	{
		super();
		my_data_store = the_data_store;
		my_role = the_role;
		my_conference = the_conference;
		my_user = the_user;
		setTitle("Assign Reviewer to Manuscript");
		setLayout(null);
		setVisible(true);
		setPreferredSize(new Dimension(800,600));
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setResizable(false);
		setupGUI();
		pack();
	}
	private void setupGUI()
	{
        JLabel selectRole = new JLabel("Select reviewer to assign to a manuscript");
        add(selectRole);
        //set the bounds of the stuff bla bla bla
        my_users.setBounds(10, 50, 300, 30);
        selectRole.setBounds(my_users.getBounds().x + 5,my_users.getBounds().y - 15, 350, 15);
        my_users.addItem("Select a reviewer");
        //Add all the users from the current conference.
        for (int i = 0; i < my_data_store.getCurrentConference().getUsers().size(); i++)
        {
        	//System.out.println(my_data_store.getCurrentConference().getUsers().get(i).getRoles().contains(Role.REVIEWER));
        	if (my_data_store.getCurrentConference().getUsers().get(i).getRoles().contains(Role.REVIEWER))
        	{
                my_users.addItem((String)my_data_store.getCurrentConference().getUsers().get(i).getName());
        	}
        }
        my_users.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	if (my_users.getSelectedItem().equals("Select a reviewer"))
            	{
            		return;
            	}
            	current_reviewer_name = (String) my_users.getSelectedItem();
            	System.out.println(current_reviewer_name);
            }
        });
        add(my_users);
        
        
        JLabel selectManuscript = new JLabel("Select Manuscript");
        add(selectManuscript);
        //set the bounds of the stuff bla bla bla
        my_manuscripts.setBounds(10, 150, 300, 30);
        selectManuscript.setBounds(my_manuscripts.getBounds().x + 5,my_manuscripts.getBounds().y - 15, 350, 15);
        my_manuscripts.addItem("Select a Manuscript");
        //Add all the users from the current conference.
        for (int i = 0; i < my_data_store.getCurrentConference().getManuscripts().size(); i++)
        {
        	my_manuscripts.addItem(my_data_store.getCurrentConference().getManuscripts().get(i).getTitle());
        }
        my_manuscripts.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	if (my_manuscripts.getSelectedItem().equals("Select a Manuscript"))
            	{
            		return;
            	}
            	current_manuscript_name = (String) my_manuscripts.getSelectedItem();
            	System.out.println(current_manuscript_name);
            }
        });
        add(my_manuscripts);
        JButton temp = ImageCreator.createButton("  Assign Reviewer to the manuscript.", new Rectangle(10,250,250,50));
		temp.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				for (int j = 0; j < my_data_store.getCurrentConference().getUsers().size(); j++)
				{
					if (my_data_store.getCurrentConference().getUsers().get(j).getName().equals(current_reviewer_name))
					{
						for (int i = 0; i < my_data_store.getCurrentConference().getManuscripts().size(); i++)
						{
							if (my_data_store.getCurrentConference().getManuscripts().get(i).getTitle().equals(current_manuscript_name))
							{
								my_data_store.getCurrentConference().getUsers().get(j).addManuscriptToReviewToList(my_data_store.getCurrentConference().getManuscripts().get(i));
								my_data_store.addConferenceObject(my_data_store.getCurrentConference());
						        JOptionPane.showMessageDialog(my_back_btn.getParent(), "You have succesfully assigned the reviewer to the manuscript.",
						                "Success!", JOptionPane.INFORMATION_MESSAGE);
							}
						}
					}
				}
			}
		});
		add(temp);
        
	//Setup back button
	my_back_btn = ImageCreator.createButton("  Go back", new Rectangle(30,520,140,40));
	my_back_btn.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			dispose(); 
			new ChooseUserStory(my_role, my_conference, my_user, my_data_store);
			
		}
	});
	add(my_back_btn);
}

}
